import { createRouter, createWebHashHistory } from "vue-router";
import store from '../store'
const routes = [
    {
        path: "/home",
        redirect: "/project",
    },
    {
        path: "/project",
        name: "Project",
        meta: {
            title: "项目配置",
            permission: '/project'
        },
        component: () => 
            import(
                "@/views/Project.vue"
            )
    },
    {
        path: "/",
        name: "Home",
        redirect: "/project",
        component: () =>
            import(/* webpackChunkName: "dashboard" */ "@/views/Home.vue"),
        children: [
            {
                path: "/triggerConfiguration/PolicyManage",
                name: "policyManage",
                meta: {
                    title: "策略列表",
                    permission: '/triggerConfiguration/PolicyManage'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/triggerConfiguration/PolicyManage.vue"
                    ),
            },
            {
                path: "/triggerConfiguration/PolicyGroupManage",
                name: "PolicyGroupManage",
                meta: {
                    title: "策略组管理",
                    permission: '/triggerConfiguration/PolicyGroupManage'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/triggerConfiguration/PolicyGroupManage.vue"
                    ),
            },

            {
                path: "/triggerConfiguration/TriggerStrategyConfiguration",
                name: "TriggerStrategyConfiguration",
                meta: {
                    title: "Trigger策略配置",
                    permission: '/triggerConfiguration/TriggerStrategyConfiguration'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSetting" */ "@/views/triggerConfiguration/TriggerStrategyConfiguration.vue"
                    ),
            },
            {
                path: "/triggerConfiguration/ConfigurationComparison",
                name: "configurationComparison",
                meta: {
                    title: "车辆动态配置对比",
                    permission: '/triggerConfiguration/ConfigurationComparison'
                },
                props: true,
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/triggerConfiguration/ConfigurationComparison.vue"
                    ),
            },
            {
                path: "/triggerConfiguration/RecycleBin",
                name: "recycleBin",
                meta: {
                    title: "策略回收站",
                    permission: '/triggerConfiguration/RecycleBin'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/triggerConfiguration/RecycleBin.vue"
                    ),
            },
            // {
            //     path: "/carDynamicSetting/NoTriggerStrategyConfiguration",
            //     name: "NoTriggerStrategyConfiguration",
            //     meta: {
            //         title: "非Trigger策略配置",
            //         permission: '/carDynamicSetting/NoTriggerStrategyConfiguration'
            //     },
            //     component: () =>
            //         import(
            //             /* webpackChunkName: "DynamicSetting" */ "@/views/triggerConfiguration/NoTriggerStrategyConfiguration.vue"
            //         ),
            // },

         
        


            {
                path: "/triggerConfiguration/CreatePolicy",
                name: "CreatePolicy",
                meta: {
                    title: "创建新策略",
                    permission: '/triggerConfiguration/CreatePolicy'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/triggerConfiguration/CreatePolicy.vue"
                    ),
            },

            {
                path: "/triggerConfiguration/UpdatePolicy",
                name: "UpdatePolicy",
                meta: {
                    title: "更新策略",
                    permission: '/triggerConfiguration/UpdatePolicy'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/triggerConfiguration/UpdatePolicy.vue"
                    ),
            },
            {
                path: "/triggerConfiguration/CreateGroupPolicy",
                name: "CreateGroupPolicy",
                meta: {
                    title: "创建组策略",
                    permission: '/triggerConfiguration/CreateGroupPolicy'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/triggerConfiguration/CreateGroupPolicy.vue"
                    ),
            },
            {
                path: "/triggerConfiguration/UpdateGroupPolicy",
                name: "UpdateGroupPolicy",
                meta: {
                    title: "更新组策略",
                    permission: '/triggerConfiguration/UpdateGroupPolicy'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/triggerConfiguration/UpdateGroupPolicy.vue"
                    ),
            },
            // 文件类别配置
            {
                path: "/cdnManage/FileBusinessConfiguration",
                name: "FileBusinessConfiguration",
                meta: {
                    title: "文件类别配置",
                    permission: '/cdnManage/FileBusinessConfiguration'
                },
                component: () =>
                    import(
                        "@/views/cdnManage/FileBusinessConfiguration.vue"
                    ),
            },
            // 文件列表
            {
                path: "/cdnManage/FileList",
                name: "FileList",
                meta: {
                    title: "文件列表",
                    permission: '/cdnManage/FileList'
                },
                component: () =>
                    import(
                        "@/views/cdnManage/FileList.vue"
                    ),
            },
            // 业务trigger关联
            {
                path: "/cdnManage/BizTriggerRelevancy",
                name: "BizTriggerRelevancy",
                meta: {
                    title: "业务trigger关联",
                    permission: '/cdnManage/BizTriggerRelevancy'
                },
                component: () =>
                    import(
                        "@/views/cdnManage/BizTriggerRelevancy.vue"
                    ),
            },

            {
                path: "/queryManager/CarStrategyDetail",
                name: "CarStrategyDetail",
                meta: {
                    title: "车辆查询策略",
                    permission: '/queryManager/CarStrategyDetail'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/queryManage/CarStrategyDetail.vue"
                    ),
            },
            {
                path: "/queryManager/StrategyInvolveCar",
                name: "StrategyInvolveCar",
                meta: {
                    title: "策略查询车辆",
                    permission: '/queryManager/StrategyInvolveCar'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/queryManage/StrategyInvolveCar.vue"
                    ),
            },
            {
                path: "/queryManager/CarStrategyConfigDetail",
                name: "CarStrategyConfigDetail",
                meta: {
                    title: "车辆历史配置",
                    permission: '/queryManager/CarStrategyConfigDetail'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/queryManage/CarStrategyConfigDetail.vue"
                    ),
            },

            {
                path: "/queryManager/OperationRecords",
                name: "OperationRecords",
                meta: {
                    title: "策略修改记录",
                    permission: '/queryManager/OperationRecords'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/queryManage/OperationRecords.vue"
                    ),
            },
            {
                path: "/queryManager/HistoricalConfiguration",
                name: "HistoricalConfiguration",
                meta: {
                    title: "历史配置查询",
                    permission: '/queryManager/HistoricalConfiguration'
                },
                component: () =>
                    import(
                        /* webpackChunkName: "DynamicSettingComparing" */ "@/views/queryManage/HistoricalConfiguration.vue"
                    ),
            },
            // 车辆日志上传
            {
                path: "/logManage/VinLogConfiguration",
                name: "VinLogConfiguration",
                meta: {
                    title: "业务trigger关联",
                    permission: '/logManage/VinLogConfiguration'
                },
                component: () =>
                    import(
                        "@/views/logConfiguration/VinLogConfiguration.vue"
                    ),
            },
            // 车辆管理
            {
                path: "/carManage/carConfiguration",
                name: "CarConfiguration",
                meta: {
                    title: "车辆查询",
                    permission: '/carManage/carConfiguration'
                },
                component: () =>
                    import(
                        "@/views/carConfiguration/CarConfiguration.vue"
                    ),
            },


            {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import(/* webpackChunkName: "403" */ '@/views/403.vue')
            },
        ],
    },

];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach(async (to, from, next) => {
    document.title = `智驾平台中心 | ${to.meta.title} `;
    const role = localStorage.getItem("ms_userName");
    let userInfo;

    try {
        userInfo = localStorage.getItem("userInfo");
    } catch (e) {
    }

    const ENV = import.meta.env;
    if (ENV.VITE_NAME_MODE === "dev") {
        next()
    } else {
        if (!role || !userInfo) {
            if (ENV.VITE_NAME_MODE === "dev") {
                window.location.href = 'https://autodrive-test.hozonauto.com/#/login';
            } else if (ENV.VITE_NAME_MODE === "test") {
                window.location.href = 'https://autodrive-test.hozonauto.com/#/login';
            } else if (ENV.VITE_NAME_MODE === "pro") {
                window.location.href = 'https://autodrive.hozonauto.com/#/login';
            } else if (ENV.VITE_NAME_MODE === "beta") {
                window.location.href = 'https://autodrive-beta.hozonauto.com/#/login';
            }
        } else if (to.meta.permission) {
            // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
            let userInfo = window.localStorage.getItem('userInfo');
            let user = JSON.parse(userInfo || '{}');
            // 若预发布环境则clientId为client0005，其他环境为88833d0124c344278124956310e0b888
            const permissionList = await store.dispatch('getPermissionList', { clientId: ENV.VITE_CLIENT_ID, userId: user.id })
            permissionList.find(fItem => fItem.path == to.meta.permission && fItem.state && fItem.type == 2) ? next() : next('/403');
        } else {
            next()
        }
    }
});

export default router;
